package net.itheima.mm.controller;

import lombok.extern.slf4j.Slf4j;
import net.itheima.mm.entity.Result;
import net.itheima.mm.pojo.User;
import net.itheima.mm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author zhangyan
 * @date 2021/2/25 15:20
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public Result login(@RequestBody User user,
                        HttpSession httpSession) {

        log.debug("登录名:" + user);
        user = userService.login(user);

        if (user != null) {
            //存储用户信息
            httpSession.setAttribute("user",user);

            return new Result(true, "登陆成功!", user);

        } else {
            return new Result(false, "用户名或密码不存在!");
        }
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession httpSession) {
            log.debug("退出");
            //存储用户信息
            httpSession.removeAttribute("user");

            return new Result(true, "退出登陆成功!");
    }
}
